﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Petaframe.DataAccess
{
    public class SqlInsertCommandBuilder : SqlCommandStringBuilderBase, ISqlCommandStringBuilder
    {      
      

        public ISqlCommandStringBuilder CommandString(string text)
        {
            _sqlQuery.Append(text);
            return this;
        }

        public ISqlCommandStringBuilder CommandString(TableInfo table)
        {
            _sqlQuery.AppendFormat("insert into {0} (", table.TableName);

            bool commaNeeded = false;
            foreach (var item in table.Properties)
            {
                if (!item.AutoIncrement)
                {
                    if (commaNeeded)
                        _sqlQuery.Append(StringTools.CommaSpace);

                    _sqlQuery.Append(item.PropertyName);
                    commaNeeded = true;
                }
            }

            _sqlQuery.Append(") values (");

           
            commaNeeded = false;
            foreach (var item in table.Properties)
            {
                //eğer autoincrement bir property değilse sorguya ekle
                if (!item.AutoIncrement)
                {                   
                    if (commaNeeded)
                        _sqlQuery.Append(StringTools.CommaSpace);

                    _sqlQuery.Append(table.Parameters[item.PropertyName].DbParameter.ParameterName);
                    commaNeeded = true;                
                }

            }

            _sqlQuery.Append(")");
            return this;
        }


        /// <summary>
        /// Oluşturulan sql komutunu döndürür.
        /// </summary>
        /// <returns>Sql command string</returns>
        public string ToSqlCommandString()
        {
            return _sqlQuery.ToString();
        }
    }
}
